package top.jacktgq.sxt.bio.ChatRoom.version2;

import java.io.*;
import java.net.Socket;

/**
 * @Author CandyWall
 * @Date 2019/10/22--18:44
 * @Description  在线聊天室：客户端
 *  *            目标：实现一个客户端可以正常收发多条消息
 */
public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("客户端启动...");
        //1、建立连接：使用Socket创建客户端+服务的端口
        Socket client = new Socket("localhost", 8888);
        //2、客户端发送消息
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        DataInputStream dis = new DataInputStream(client.getInputStream());
        Boolean isRunning = true;
        while (isRunning) {
            System.out.print("请输入用户名：");
            String username = br.readLine();
            System.out.print("请输入密码：");
            String password = br.readLine();
            dos.writeUTF("username=" + username + "&password=" + password);
            dos.flush();

            //3、获取服务器端发来的消息
            String content = dis.readUTF();
            System.out.println("服务器端发来：" +content);
        }

        //释放资源
        dos.close();
        dis.close();
        client.close();
    }
}
